<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    主要是根据是否包含来去重，
    或者使用new Set函数自动去重

    <script>
        //1.使用new Set()
        var arr = [1, 1, 'true', 'true', true, true, 15, 15, false, false, undefined, undefined, null, null, NaN, NaN,
            'NaN', 0, 0, 'a', 'a', {}, {}
        ];

        function unique(arr) {
            return Array.from(new Set(arr))
        }

        function unique1(arr) {
            for (var i = 0; i < arr.length; i++) {
                for (var j = i + 1; j < arr.length; j++) {
                    if (arr[i] == arr[j]) { //第一个等同于第二个，splice方法删除第二个
                        arr.splice(j, 1);
                        j--;
                    }
                }
            }
            return arr;
        }

        function unique2(arr) {
            if (!Array.isArray(arr)) {
                console.log('type error!')
                return
            }
            var array = [];
            for (var i = 0; i < arr.length; i++) {
                if (array.indexOf(arr[i]) === -1) {
                    array.push(arr[i])
                }
            }
            return array;
        }

        function unique3(arr) {
            if (!Array.isArray(arr)) {
                console.log('type error!')
                return;
            }
            arr = arr.sort()
            var arrry = [arr[0]];
            for (var i = 1; i < arr.length; i++) {
                if (arr[i] !== arr[i - 1]) {
                    arrry.push(arr[i]);
                }
            }
            return arrry;
        }

        function unique4(arr) {
            let total = []
            arr.forEach(function (item) {
                if (!total.includes(item)) {
                    total.push(item)
                }
            })
            return total
        }

        function unique5(arr) {
            let map = new Map();
            let array = new Array(); // 数组用于返回结果
            for (let i = 0; i < arr.length; i++) {
                if (map.has(arr[i])) { // 如果有该key值
                    map.set(arr[i], true);
                } else {
                    map.set(arr[i], false); // 如果没有该key值
                    array.push(arr[i]);
                }
            }
            return array;
        }
        console.log(unique(arr))
        let ddd = [...new Set(arr)]
        console.log(ddd)
        // console.log(unique1(arr))
        console.log(unique2(arr))
        console.log(unique3(arr))
        console.log(unique4(arr))
        console.log(unique5(arr))
        let arr2 = [false, true, undefined, null, NaN, 0, 1, {}, {}, 'a', 'a', NaN]

        function unique6() {
            let arr = [...arguments]
            let obj = {}
            let newArr = []
            for (let i = 0; i < arr.length; i++) {
                if (!obj[arr[i]]) {
                    obj[arr[i]] = arr[i]
                    newArr.push(arr[i])
                }
            }
            console.log(obj)
            return newArr
        }
        console.log(unique6(arr2))
    </script>
</body>

</html>